﻿#include "mydb.h"
#include "json11.hpp"
#include <iostream>
#include <list>
#include <map>

MyDB::MyDB() {
    mysql = mysql_init(NULL);
    if(mysql == NULL) {
        cout << "Error: " << mysql_error(mysql);
        exit(-1);
    }
    if(!InitDB(HOST, USER, PWD, DBNAME, PORT)) {
        cout << "InitDB error" << endl;
        exit(-1);
    }
    cout << "db init success" << endl;
}

MyDB::~MyDB() {
    if(!MyDB::mysql) {
        mysql_close(MyDB::mysql);
    }
}

bool MyDB::InitDB(const char *host, const char *user, const char *pwd, const char *dbname, int port) {
    /*连接数据库*/
    if(!mysql_real_connect(mysql, host, user, pwd, dbname, port, NULL, 0)) {
        cout << "connect fial: " << mysql_error(mysql);
        return false;
    }
    //设置数据库编码类型
//    if(0 == mysql_options(mysql, MYSQL_SET_CHARSET_NAME, "utf8")) {
//        cout << "设置数据库编码类型成功！" << endl;
//    } else {
//        cout << "设置数据库编码类型失败！" << endl;
//        return -1;
//    }
    return true;
}

string *MyDB::selectData(string *sql, string *resultStr) {
    //设置编码格式utf8
    if(mysql_query(mysql, "set names utf8")) {
        cout << "set fail: " << mysql_error(mysql);
        return NULL;
    }
    /*执行失败*/
    if(mysql_query(MyDB::mysql, sql->c_str())) {
        cout << "query fail: " << mysql_error(mysql);
        return NULL;
    } else {
        MYSQL_ROW row;  // 查询结果的行
        MYSQL_RES* result;  // 全部查询结果
        //MYSQL_FIELD* field;  // 查询结果字段
        //一次性取得数据集
        result = mysql_store_result(mysql);

        //取得并打印行数
        int rowcount = mysql_num_rows(result);
        cout << "row count: " << rowcount << endl;

        //取得并打印各字段的名称
        int fieldcount = mysql_num_fields(result);
        MYSQL_FIELD *field = NULL;

        for (int i = 0; i < fieldcount; i++) {
            field = mysql_fetch_field_direct(result, i);
            cout << field->name << "\t\t";
        }
        cout << endl;


        list<map<string, string>> list1;
        map<string, list<map<string, string>>> map2;

        //打印各行
        while(row = mysql_fetch_row(result)) {
            map<string, string> map1;
            for(int i = 0; i < fieldcount; i++) {
                field = mysql_fetch_field_direct(result, i);
                if(row[i] == NULL) {
                    map1[field->name] = "";
                    continue;
                }
                map1[field->name] = row[i];
//                cout << row[i] << "\t";
            }
            list1.push_back(map1);
            cout << endl;
        }
        //map2["result"] = list1;
        json11::Json json(list1);
        *resultStr = json.dump();
        //cout << jsonStr->c_str() << endl;
        cout << "============================" << endl;

        mysql_free_result(result);

        return resultStr;
    }
}

bool MyDB::updateData(string *sql) {
    //设置编码格式utf8
    if(mysql_query(mysql, "set names utf8")) {
        cout << "set fail: " << mysql_error(mysql);
        return false;
    }
    if(mysql_query(MyDB::mysql, sql->c_str())) {
        cout << "update fail: " << mysql_error(mysql);
        return false;
    }
    return true;
}
